<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('session');
    }
    public function index() {
        if($this->session->userdata('barangay_id') != '' || $this->session->userdata('barangay_id') != null) {
            $this->load->model('News_model');
            $data['news'] = $this->News_model->getAll();
            $this->load->view('news_index', $data);
        } else {
            header('Location: ' . base_url());
        }
    }
    public function get($type = '') {
        $this->load->model('News_model');
        $data['news'] = $this->News_model->get($this->input->post('getSome'));
        print json_encode($data);
    }
    public function topfivenews() {
        $this->load->model('News_model');
        $result['news'] = $this->News_model->topFiveNews();
        print json_encode($result);
    }
    public function macroView() {
        print $this->input->get('news_id');
    }
    public function crudNews() {
         if($this->session->userdata('user_resp') == 'admin') {
            if($this->input->post('l_submit')=='y') {
                $this->load->model('News_model');
                if($this->input->post('l_id')=='' || $this->input->post('l_id')==null) {
                    $data['l_res1'] = $this->News_model->addNews();
                    print $data['l_res1'];
                } else {
                    $data['l_res2'] = $this->News_model->updateNews();
                    print $data['l_res2'];
                    //print json_encode($data);
                    //print 'this should have been erred';
                }
            } else {
                if($this->input->get('news_id_pk')) {
                    $this->load->model('News_model');
                    $data['news'] = $this->News_model->getNewsId($this->input->get('news_id_pk'));
                    if(count($data['news']) == 0) {
                        $this->load->view('news_crud');
                    } else {
                        $this->load->view('news_crud', $data);
                    }
                } else {
                    $this->load->view('news_crud');
                }
            }
        } else {
            header('Location: ' . base_url() . 'index.php/news');
        }
    }
        public function deletenews() {
        if($this->session->userdata('user_resp') == 'admin') {
            if($this->input->post('news_id_pk')) {
                $this->load->model('News_model');
                $result = $this->News_model->deleteNews($this->input->post('news_id_pk'));
                print json_encode($result);
            } else {
                return false;
            }
        } else {
            header('Location: ' . base_url() . 'index.php/news');
        }
    }
}
